package com.personnel.controller;

import com.personnel.mapper.DepartmentMapper;
import com.personnel.mapper.JobMapper;
import com.personnel.mapper.StaffMapper;
import com.personnel.pojo.Department;
import com.personnel.pojo.Job;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: YuePeng
 * @Date: 2022/3/9 15:35
 * @Version: 1.0
 */
@RestController
@RequestMapping("depAndJob")
public class DepAndJobController {
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    @GetMapping("jobTree")
    @ResponseBody
    public List<Map<String, Object>> jobTree() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> eachJobList = new ArrayList<>();
        List<Department> departments = this.departmentMapper.selectAll();
        for (Department department : departments) {
            Map<String, Object> depMap = new HashMap<>();
            depMap.put("id",department.getDepartmentId());
            depMap.put("jbId",department.getDepartmentId());
            depMap.put("title",department.getDepartmentName());
            List<Job> jobList = this.jobMapper.selectByDepartmentId(department.getDepartmentId() + "");
            for (Job job : jobList) {
                Map<String, Object> jobMap = new HashMap<>();
                jobMap.put("id",job.getJobId());
                jobMap.put("title",job.getJobName());
                jobMap.put("jdId",job.getJobDepartmentId());
                jobMap.put("jdName",job.getDepartment().getDepartmentName());
                eachJobList.add(jobMap);
            }
            depMap.put("children",eachJobList);
            depMap.put("spread",true);
            eachJobList = new ArrayList<>(); //清空一次，不然每个depMap都有全部职位
            list.add(depMap);
        }
        return list;
    }

    @GetMapping("depTree")
    @ResponseBody
    public List<Map<String, Object>> depTree() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Department> departments = this.departmentMapper.selectAll();
        for (Department department : departments) {
            Map<String, Object> depMap = new HashMap<>();
            depMap.put("id",department.getDepartmentId());
            depMap.put("title",department.getDepartmentName());
            list.add(depMap);
        }
        return list;
    }
}
